<template>
	<div class="set_nickname">
		<van-cell-group>
			<van-field
			v-model="nickName"
			label="姓名"
			:error="!!$vuelidation.error('nickName')" />
		</van-cell-group>
		
		<div class="bottom_btn">
			<van-button size="large" type="danger" @click="saveNick">保存</van-button>
		</div>
	</div>
</template>


<script>
	import { USER_MODIFY } from '@/api/user';
	import valid from '@/api/user';
	
	export default {
		data(){
			return {
				nickName: "",
				id: -1
			}
		},
		
		vuelidation: {
			data: {
				nickName: {
					required: {
						msg(){
							return "请填写姓名"
						}
					}
				}
			}
		},
		
		created(){
			this.getNick();
		},
		
		methods: {
			getNick(){
				this.nickName = this.user.realname || '';
				this.id = this.user.id;
			},
			saveNick(){
				if(this.mixValid()){
					this.$reqPost(USER_MODIFY, {id: this.id, realname: this.nickName}).then(res => {
						 return this.$dialog.alert({message: '保存成功'})
					}).then(() => {
						this.$router.go(-1);
					})
				}
			},
		}
	}
</script>


<style scoped>
	.bottom_btn{
		padding: 30px 15px 0 15px; 
	}
</style>